﻿<#
.SYNOPSIS  
    Tears down a SharePoint 2010 farm so that it can be upgraded again using Upgrade-Farm.ps1.  
.DESCRIPTION  
    This script executes the tear down sequence in the correct order using configuration information in the Upgrade.xml file.
    When it completes successfully, the farm will be ready for another practice upgrade.
    You can schedule this script to run on a periodic basis to practice upgrade.  When scheduling upgrade, the sequence should be
    TearDown-Farm.ps1, SQL job to backup and restore SharePoint 2007 databases, Upgrade-Farm.ps1.
    After tear down, you should review the following log files for errors:
        TearDown-Farm.log
.NOTES
    Pre-conditions:
        Upgrade.xml contains correct configuration (dev / prod / sat, managed accounts, database servers, database names etc)
        This script should be run as the SharePoint farm account
.LINK  
    http://7up.codeplex.com 
#>

#gets the script directory
function Get-ScriptDirectory
{
	$Invocation = (Get-Variable MyInvocation -Scope 1).Value
	Split-Path $Invocation.MyCommand.Path
}

#set current directory
cd (Get-ScriptDirectory)

Start-Transcript -Path ".\TearDown-Farm.log"

Write-Host ("Tearing down the farm ({0})..." -f [DateTime]::Now.ToString() )

#restart all services to remove any file locks
.\Stop-AllSharePointServices
.\Start-AllSharePointServices

#remove old log files
.\Remove-UpgradeLogFiles

#remove project web instances
.\Remove-ProjectWebInstances

#remove project service applications
.\Remove-ProjectServiceApplications

#remove profile service applications
.\Remove-ProfileServiceApplications

#remove web applications (and content databases)
.\Remove-WebApplications

#remove entries to hosts file
.\Remove-HostsFileEntries

#remove custom solutions
.\Remove-Solutions

Write-Host ("Farm tear down complete ({0})." -f [DateTime]::Now.ToString() )

Stop-Transcript

#clean up the transcript file (start-transcript doesn't format linebreaks)
[string]::Join("`r`n",(Get-Content ".\TearDown-Farm.log")) | Out-File ".\TearDown-Farm.log"